/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.inspur.edp.lcm.metadata.core.thread;

import com.inspur.edp.lcm.metadata.inner.api.data.PublishStatus;
import com.inspur.edp.lcm.metadata.inner.api.enm.StageEnum;
import com.inspur.edp.lcm.metadata.inner.api.utils.IdeLogUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Data
public class MockWebProcessTask implements Runnable {
    /**
     * 退出标识位
     */
    public volatile boolean exit = false;

    /**
     * 请求标识
     */
    private String requestId;

    /**
     * 最小进度
     */
    private int minProcess = 0;
    /**
     * 最大进度
     */
    private int maxProcess = 0;

    private MockWebProcessTask() {

    }

    public MockWebProcessTask(String requestId, int minProcess, int maxProcess) {
        this.requestId = requestId;
        this.minProcess = minProcess;
        this.maxProcess = maxProcess;
    }

    @Override
    public void run() {
        for (int count = 0; count <= 120; count++) {
            try {
                Thread.sleep(1000);
                if(exit){
                    break;
                }else{
                    IdeLogUtils.syncProcess(requestId,
                        PublishStatus.success(minProcess + (maxProcess - minProcess) * count / 120, StageEnum.EXTEND_COMPILE.getCode())
                    );
                }
            } catch (InterruptedException e) {
                log.warn("前端编译实时发送进度过程发生等待中断异常:", e);
            }
        }
    }

    public static MockWebProcessTask ofMockWebProcessTask(String requestId, int minProcess, int maxProcess) {
        return new MockWebProcessTask(requestId, minProcess, maxProcess);
    }
}
